<p>This test tabs with different combinations of modifiers pressed, where some should advance focus and some should not.</p>
<div id="testDiv"></div>
<pre id="console"></pre>

<script>

if (window.testRunner)
    testRunner.dumpAsText();

function log(s)
{
    document.getElementById('console').appendChild(document.createTextNode(s));
}

function description(element)
{
    return element.getAttribute("id");
}

function keydownListener(event)
{
    log("- keydown event: " + description(event.target)  + "\n");
}

function blurListener(event)
{
    log("- blur event: " + description(event.target) + "\n");
}

var lastFocusedElement = null;
function focusListener(event)
{
    log('- focus event: ' + description(event.target) + '\n');
    lastFocusedElement = event.target;
}

function addEventListeners(element)
{
    element.addEventListener('keydown', keydownListener, false);
    element.addEventListener('focus', focusListener, false);
    element.addEventListener('blur', blurListener, false);
}

function addElements(parentElement)
{
    // Add 3 input elements
    var input = parentElement.ownerDocument.createElement('input');
    addEventListeners(input);
    input.setAttribute("id", "a");
    parentElement.appendChild(input);
    input = parentElement.ownerDocument.createElement('input');
    addEventListeners(input);
    input.setAttribute("id", "b");
    parentElement.appendChild(input);
    input = parentElement.ownerDocument.createElement('input');
    addEventListeners(input);
    input.setAttribute("id", "c");
    parentElement.appendChild(input);
}

function dispatchTab(element, shiftKey, metaKey, ctrlKey)
{
    var modifiers = [];
    if (shiftKey)
        modifiers.push("shiftKey");
    if (metaKey)
        modifiers.push("metaKey");
    if (ctrlKey)
        modifiers.push("ctrlKey");

    eventSender.keyDown('\u0009', modifiers);
}

function setup()
{
    var testDiv = document.getElementById('testDiv');
    addElements(testDiv);
}

setup();

var middleInput = document.getElementsByTagName('input')[1];

for (i = 0; i < 2; ++i) {
    log("resetting focus to middle input\n");
    middleInput.focus();
    log("test tab case that should advance focus\n");
    dispatchTab(middleInput, (i & 1) != 0, false, false);
}
for (i = 2; i < 8; ++i) {
    log("resetting focus to middle input\n");
    middleInput.focus();
    log("test tab case that should not advance focus\n");
    dispatchTab(middleInput, (i & 1) != 0, (i & 2) != 0, (i & 4) != 0);
}

</script>
